<template>
  <div class="geek-article">
    <!-- 内容概览 -->
    <el-card shadow="always">
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>活动管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-form label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="reqParams.status">
            <!-- 文章状态，0-草稿，1-待审核，2-审核通过，3-审核失败，不传为全部 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <GeekChannel v-model="reqParams.channel_id" />
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dataArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="filterList()">登录</el-button>
          {{ reqParams }}
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="always" class="article-down">
      <div slot="header">
        <p>根据筛选条件共查询到 {{ total }} 条结果：</p>
      </div>
      <!-- 表格 -->
      <el-table :data="articleList">
        <!-- 封面 标题 状态 发布时间 阅读数 评论数 点赞数 操作 -->
        <el-table-column label="封面">
          <template slot-scope="scope">
            <!-- <img :src="scope.row.cover.images[0]" alt="" style="width: 100px; height: 100px;"> -->
            <el-image
              :src="scope.row.cover.images[0]"
              :preview-src-list="scope.row.cover.images"
              style="width: 100px; height: 100px"
              fit="cover"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">草稿</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="info"
              >待审核</el-tag
            >
            <el-tag v-else-if="scope.row.status === 2">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="danger"
              >审核失败</el-tag
            > </template
          >"
        </el-table-column>
        <el-table-column
          label="发布时间"
          prop="pubdate"
          width="200"
        ></el-table-column>
        <el-table-column label="阅读数" prop="read_count"></el-table-column>
        <el-table-column label="评论数" prop="comment_count"></el-table-column>
        <el-table-column label="点赞数" prop="like_count"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              circle
              icon="el-icon-edit"
              @click="changeArticle(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              plain
              circle
              icon="el-icon-delete"
              @click="DelArticle(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px"
        :current-page.sync="reqParams.page"
        :page-sizes="[2, 4, 6, 8]"
        :page-size.sync="reqParams.per_page"
        @current-change="getArticles()"
        @size-change="getArticles()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      reqParams: {
        status: null,
        channel_id: null,
        begin_pubDate: null,
        end_pubDate: null,
        page: 1,
        per_page: 2
      },
      channelOptions: [],
      dataArr: [],
      articleList: [],
      total: 0
    }
  },
  watch: {
    // 监听dataArr的变化，当dataArr为空时，
    // 将reqParams的begin_pubDate和end_pubDate置为null
    dataArr: {
      deep: true,
      handler (value) {
        if (value === null) {
          this.reqParams.begin_pubDate = null
          this.reqParams.end_pubDate = null
        } else {
          // value就是dataArr，value[0]是开始日期，value[1]是结束日期
          this.reqParams.begin_pubDate = value[0]
          this.reqParams.end_pubDate = value[1]
        }
      }
    }
  },
  created () {
    // 获取频道列表
    // this.getChannels()
    this.getArticles()
  },
  methods: {
    // 获取文章列表
    async getArticles () {
      const res = await this.$http.get('/v1_0/mp/articles', {
        params: this.reqParams
      })
      this.articleList = res.data.data.results
      this.total = res.data.data.total_count
    },
    // 筛选列表
    filterList () {
      this.reqParams.page = 1
      this.getArticles()
    },
    // 删除文章
    DelArticle (id) {
      this.$confirm('此操作将永久删除该文章吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await this.$http.delete(`/v1_0/mp/articles/${id}`)
          this.getArticles()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改文章
    changeArticle (id) {
      this.$router.push(`/publish/${id}`)
    }
  }
}
</script>
<style>
.article-down {
  margin-top: 10px;
}
</style>
